package br.ufpe.cin.mac.demo.ImageProcesser.server;

import java.awt.RenderingHints;
import java.awt.image.renderable.ParameterBlock;
import java.io.File;
import java.rmi.RemoteException;

import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;

import br.ufpe.cin.mac.middleware.App;
import br.ufpe.cin.mac.middleware.Middleware;
import br.ufpe.cin.mac.util.Message;
import br.ufpe.cin.mac.util.Proxy;

import com.sun.media.jai.rmi.ImageServer;
import com.sun.media.jai.rmi.JAIRMIImageServer;

public class ImageProServer implements App, IImageProServer{

	public ImageServer server = null;
	
	public ImageProServer(int i) {
		super();	
		try {
			this.server = new JAIRMIImageServer(i);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
/* (non-Javadoc)
 * @see br.ufpe.cin.mac.demo.ImageProcesser.server.IFaceImgProServer#Notify(br.ufpe.cin.mac.util.Message)
 */
	public void Notify(Message msg) {
		System.out.println("\n---Notify---\n");
		String strMsg = msg.toString();
		System.out.println(strMsg);
		System.out.println("-------------\n");
	}
  
   

	/* (non-Javadoc)
	 * @see br.ufpe.cin.mac.demo.ImageProcesser.server.IFaceImgProServer#getDirectoryListing(java.lang.String)
	 */
    public String[] getDirectoryListing(String pathToDirectory) {
    	System.out.println("\n---Get Path---");
    	System.out.println("Image Path: " + pathToDirectory);
    	System.out.println("\n--------------");
    	
    	String[] ret;
    	boolean fail = false;
    	File directory = new File(pathToDirectory);

    	if (directory.exists() == false){ 
    		System.err.println("Specified path name does not exist.");
    		fail = true;
    	}    	
    	if (directory.isDirectory() == false){ 
    		System.err.println("Specified path name is not a directory");
    		fail = true;    		
    	}
    	if(fail){
    		ret =  new String[0];
    	}else{
    		ret = directory.list(new ImageFilenameFilterImpl());
    	}
	
    	return ret;
    }

    /* (non-Javadoc)
	 * @see br.ufpe.cin.mac.demo.ImageProcesser.server.IFaceImgProServer#getPathDirectoryAbsoluteListing(java.lang.String)
	 */
    public String[] getPathDirectoryAbsoluteListing(String pathToDirectory) {
    	System.out.println("\n---Get Absolute Path---");
    	System.out.println("Image Path: " + pathToDirectory);
    	System.out.println("\n-----------------------");
    	
    	String[] ret;
    	boolean fail = false;
    	File directory = new File(pathToDirectory);

    	if (directory.exists() == false){ 
    		System.err.println("Specified path name does not exist.");
    		fail = true;
    	}    	
    	if (directory.isDirectory() == false){ 
    		System.err.println("Specified path name is not a directory");
    		fail = true;    		
    	}
    	if(fail){
    		ret =  new String[0];
    	}else{
    		File[] files = directory.listFiles(new ImageFilenameFilterImpl());
    		ret = new String[files.length];
    		for (int i=0; i<files.length; i++) {
    		    ret[i] = files[i].getAbsolutePath();
    		}
    	}
    	
    	return ret;

    }
    
    /* (non-Javadoc)
	 * @see br.ufpe.cin.mac.demo.ImageProcesser.server.IFaceImgProServer#create(java.lang.String, java.awt.image.renderable.ParameterBlock, java.awt.RenderingHints)
	 */
    public RenderedOp create(String opName, ParameterBlock args, RenderingHints hints) {
    	System.out.println("\n---Create---");    	
    	System.out.println("Operation: " + ((opName==null ) ? "Null" : opName) );
    	System.out.println("ParameterBlock: " + ((args==null ) ? "Null" : args.toString()) );
    	System.out.println("RenderingHints: " + ((hints==null ) ? "Null" : hints.toString()) );
    	System.out.println("\n------------"); 
    	RenderedOp im = JAI.create(opName, args, hints);
		return im;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Middleware mid = new Middleware("server", "Config2.xml");
		new Proxy(new ImageProServer(0), mid);
	}

}
